package com.hna.eking.AirlineServer.Platform.Airport;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hna.eking.AirlineServer.Mapper.AirportInfo;
import com.hna.eking.AirlineServer.Mapper.AirportInfoMapper;
import com.hna.eking.AirlineServer.Mapper.MapInfo;
import com.hna.eking.AirlineServer.Mapper.MapInfoMapper;
import com.hna.eking.AirlineServer.Utils.ResponseSupport;

//@Controller
@RestController
public class CommonController {
	
	Logger logger = LoggerFactory.getLogger(CommonController.class);
	
	private SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Autowired
	AirportInfoMapper airportDao;
	@Autowired
	MapInfoMapper mapDao;
	
	/*
	 * maxDate: 最大日期
	 * downloaded :json数组格式的字符串，[{'airportNo': 'A111', 'mapVersion': '10'}, {...}]
	 * */
	@RequestMapping(value="/airports", method= {RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody ResponseSupport<Map<String,Object>> airportInfos(
			@RequestParam(value="maxVersion",required=false) String maxVersion,
			@RequestParam(value="downloaded",required=false) String downloaded) {
		ResponseSupport<Map<String,Object>> responseSupport  = new ResponseSupport<Map<String,Object>>();
		responseSupport.setCode(200);
		
		List<AirportInfo> downArpts = new ArrayList<AirportInfo>();
		List<AirportInfo> updArpts = airportDao.selectByAirportVersion(Integer.parseInt(maxVersion.length() == 0 ? "-1" : maxVersion));
		try {
			ObjectMapper mapper = new ObjectMapper();
			JsonNode rootNode = null;
			rootNode = mapper.readTree(downloaded);
			System.out.println(downloaded);
			for (JsonNode node : rootNode) {
				System.out.println(node.isArray());
				String airportNo = node.path("airportNo").textValue();
				String mapVersion = node.path("mapVersion").textValue();
				AirportInfo airport  = airportDao.selectByAirportCodeAndMapVersion(airportNo, Integer.parseInt(mapVersion.length() == 0 ? "-1" : mapVersion));
				if (airport != null) {
					downArpts.add(airport);
				}
			}
			
		} catch (JsonProcessingException e) {
			responseSupport.setCode(1001);
		} catch (IOException e) {
			responseSupport.setCode(1001);
		}
		
		Map<String,Object> bodyMap = new HashMap<String, Object>();
		bodyMap.put("downArpts", downArpts);	//需要下载或更新航图条目的机场
		bodyMap.put("updArpts", updArpts);	//需要更新的机场条目
		responseSupport.setContent(bodyMap);
		
		return responseSupport;	
	}
	
	@RequestMapping(value="/airport/{code}", method=RequestMethod.GET)
	public @ResponseBody AirportInfo airportInfo(@PathVariable(value="code") String code) {
		AirportInfo airport = null;
		
		if (code == null) {
			
		} else {
			
		}
		return airport;	
	}
	
	
	@RequestMapping(value="/maps", method= {RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody ResponseSupport<Map<String,Object>> mapInfos(HttpServletResponse response,
			@RequestParam(value="mapVersion",required=true) String mapVersion,
			@RequestParam(value="airportNo",required=true) String airportNo,
			@RequestParam(value="effectFlag",required=false) String effectFlag){
		ResponseSupport<Map<String,Object>> responseSupport  = new ResponseSupport<Map<String,Object>>();
		responseSupport.setCode(200);
		Map<String,Object> bodyMap = new HashMap<String, Object>();
		
		mapVersion = (mapVersion == null || mapVersion.length() == 0) ? "-1" : mapVersion;
		Integer MapVersionNew = airportDao.selectMapVersion(airportNo);
		
		//最新航图版本等于传过来的版本，则无需更新。
		if (Integer.parseInt(mapVersion) == MapVersionNew) {
			bodyMap.put("mapVersion", MapVersionNew.toString());	
			bodyMap.put("maps", new ArrayList<MapInfo>());
			responseSupport.setContent(bodyMap);
			return responseSupport;
		}
		
		List<MapInfo> maps = null;
		if (effectFlag == null) {
			maps = mapDao.selectByAirportCodeAndMapVersion(airportNo, Integer.parseInt(mapVersion));
		} else {
			maps = mapDao.selectByAirportCodeAndMapVersionAndEffectFlag(airportNo, Integer.parseInt(mapVersion), effectFlag);
		}
		bodyMap.put("mapVersion", MapVersionNew.toString());	//机场表的航图版本
		bodyMap.put("maps", maps);
		responseSupport.setContent(bodyMap);
		
		return responseSupport;
	}
}
